package com.amigo.online.provider.user.util;

import java.util.ArrayList;

/**
 * 注册码生成工具
 */
public class RegisterCodeUtils {

    private static byte[] base = "0123456789ABCDEFGHJKLMNPQRSTUVWXYZ".getBytes();

    //将id映射成邀请码
    public static String generate(int id) {
        int temp = id;
        int mod;
        StringBuilder codeBuilder = new StringBuilder();
        ArrayList<Byte> list = new ArrayList<>();
        while (temp != 0) {
            mod = temp % 34;
            temp = temp / 34;
            list.add(base[mod]);
        }
        if (list.size() > 6) {
            for (int i = 0; i < 6; i++) {
                codeBuilder.append((char) list.get(i).intValue());
            }
        } else if (list.size() == 6) {
            for (int i = 0; i < 6; i++) {
                codeBuilder.append((char) list.get(5 - i).intValue());
            }

        } else {
            for (int i = 0; i < 6; i++) {
                if (6 - i > list.size()) {
                    codeBuilder.append((char) base[0]);
                } else {
                    codeBuilder.append((char) list.get(5 - i).intValue());
                }
            }
        }
        return codeBuilder.toString();
    }
    public static void main(String[] args) {
        System.out.println(new Double(Math.random()*10000000).intValue());
        for(int i = 0 ;i<10;i++){

            System.out.println(generate(new Double(Math.random()*100000000).intValue()));

        }
    }
}
